package com.xjy.web.controller.system;

import com.xjy.common.annotation.Log;
import com.xjy.common.annotation.RepeatSubmit;
import com.xjy.common.core.controller.BaseController;
import com.xjy.common.core.domain.AjaxResult;
import com.xjy.common.core.page.TableDataInfo;
import com.xjy.common.enums.BusinessType;
import com.xjy.common.exception.UtilException;
import com.xjy.common.utils.poi.ExcelUtil;
import com.xjy.system.domain.bo.SysMaterialMngAddBo;
import com.xjy.system.domain.bo.SysMaterialMngEditBo;
import com.xjy.system.domain.bo.SysMaterialMngQueryBo;
import com.xjy.system.domain.vo.SysMaterialMngVo;
import com.xjy.system.service.ISysMaterialMngService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 物料管理Controller
 *
 * @author zc
 * @date 2022-01-30
 */
@Api(value = "物料管理控制器", tags = {"物料管理管理"})
//@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/system/mng")
public class SysMaterialMngController extends BaseController {
	@Autowired
    private  ISysMaterialMngService iSysMaterialMngService;

    /**
     * 查询物料管理列表
     */
    @ApiOperation("查询物料管理列表")
    @PreAuthorize("@ss.hasPermi('system:mng:list')")
    @GetMapping("/list")
    public TableDataInfo<SysMaterialMngVo> list(@Validated SysMaterialMngQueryBo bo) {
        return iSysMaterialMngService.queryPageList(bo);
    }

    /**
     * 导出物料管理列表
     */
    @ApiOperation("导出物料管理列表")
    @PreAuthorize("@ss.hasPermi('system:mng:export')")
    @Log(title = "物料管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<SysMaterialMngVo> export(@Validated SysMaterialMngQueryBo bo) {
		List<SysMaterialMngVo> list = iSysMaterialMngService.queryList(bo);
		ExcelUtil<SysMaterialMngVo> util = new ExcelUtil<>(SysMaterialMngVo.class);
        return util.exportExcel(list, "物料管理");
    }

    /**
     * 获取物料管理详细信息
     */
    @ApiOperation("获取物料管理详细信息")
    @PreAuthorize("@ss.hasPermi('system:mng:query')")
    @GetMapping("/{id}")
    public AjaxResult<SysMaterialMngVo> getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable("id") Long id) {
        return AjaxResult.success(iSysMaterialMngService.queryById(id));
    }

    /**
     * 新增物料管理
     */
    @ApiOperation("新增物料管理")
    @PreAuthorize("@ss.hasPermi('system:mng:add')")
    @Log(title = "物料管理", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated @RequestBody SysMaterialMngAddBo bo) {
		bo.setAuditStatus(false);
        return toAjax(iSysMaterialMngService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改物料管理
     */
    @ApiOperation("修改物料管理")
    @PreAuthorize("@ss.hasPermi('system:mng:edit')")
    @Log(title = "物料管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping("/edit")
    public AjaxResult<Void> edit(@Validated @RequestBody SysMaterialMngEditBo bo) {
		SysMaterialMngVo sysMaterialMngVo = iSysMaterialMngService.queryById(bo.getId());
		if(sysMaterialMngVo.getAuditStatus()){
			throw new UtilException("该数据已经审核无法修改！");
		}
		Long startAmt = bo.getStartAmt();
		if(null == startAmt || !(startAmt >= 0)){
			throw new UtilException("期初余额必须大于等于0！");
		}
		bo.setInitAmt(bo.getStartAmt());
		bo.setAvailableStock(bo.getStartAmt());
        return toAjax(iSysMaterialMngService.updateByEditBo(bo) ? 1 : 0);
    }

	/**
	 * 审批项目基本信息
	 */
	@ApiOperation("审批项目基本信息")
	@PreAuthorize("@ss.hasPermi('system:proj:edit')")
	@Log(title = "项目基本信息", businessType = BusinessType.UPDATE)
	@PutMapping("/audit")
	public AjaxResult<Void> audit(@Validated @RequestBody SysMaterialMngEditBo bo) {
		SysMaterialMngVo sysMaterialMngVo = iSysMaterialMngService.queryById(bo.getId());
		if (sysMaterialMngVo.getAuditStatus()) {
			throw new UtilException("该数据无需重新审批！");
		}
		Long startAmt = bo.getStartAmt();
		if (null == startAmt || !(startAmt >= 0)) {
			throw new UtilException("期初余额必须大于等于0！");
		}
		bo.setAuditStatus(true);
		bo.setInitAmt(bo.getStartAmt());
		bo.setAvailableStock(bo.getStartAmt());
		return toAjax(iSysMaterialMngService.updateByEditBo(bo) ? 1 : 0);
	}

    /**
     * 删除物料管理
     */
    @ApiOperation("删除物料管理")
    @PreAuthorize("@ss.hasPermi('system:mng:remove')")
    @Log(title = "物料管理" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] ids) {
        return toAjax(iSysMaterialMngService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
